package com.example.chenhaosblog.controller;

import com.alibaba.fastjson.JSON;
import com.example.chenhaosblog.base.BaseController;
import com.example.chenhaosblog.service.AccountFlowService;
import com.example.chenhaosblog.service.AppliancesService;
import com.example.chenhaosblog.service.DemoService;
import com.example.chenhaosblog.utils.DataUtil;
import com.example.chenhaosblog.utils.DateUtil;
import com.example.chenhaosblog.utils.PageData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * demo 模块
 */
@Controller
@RequestMapping("/AppliancesController")
public class AppliancesController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(AppliancesController.class);

	@Autowired
	private AppliancesService  appliancesService;


	@Autowired
	private AccountFlowService accountFlowService;

	/**
	 *   查询数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/view")
	@ResponseBody
	public PageData findView() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {

			PageData data=	appliancesService.findView(pageData);

			result.put("code", "00");
			result.put("data",data);
			result.put("msg", "查询成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}


	/**
	 *   修改数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update")
	@ResponseBody
	public PageData update() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {
			pageData.put("updateTime", DateUtil.getTime());
			appliancesService.update(pageData);

			PageData pageData1 = new PageData();
			pageData1.put("updateTime", DateUtil.getTime());
			pageData1.put("price", pageData.getString("price"));
			pageData1.put("source_id",pageData.getString("id"));

			accountFlowService.update(pageData1);

			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "保存成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}

	/**
	 *   新增数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public PageData add() throws Exception{

		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();

		//List<PageData> list = JSON.parseArray(pageData.getString("jsonData"), PageData.class) ;

		try {
			String uuid = get32UUID();
			pageData.put("id",uuid);
			pageData.put("createTime", DateUtil.getTime());
			pageData.put("updateTime", DateUtil.getTime());
			appliancesService.add(pageData);
			PageData pageData1 = new PageData();
			pageData1.put("id",get32UUID());
			pageData1.put("createTime", DateUtil.getTime());
			pageData1.put("updateTime", DateUtil.getTime());
			pageData1.put("source_id",uuid);
			pageData1.put("price",pageData.getString("price"));
			pageData1.put("name",pageData.getString("title"));
			pageData1.put("type","1");
			accountFlowService.add(pageData1);


			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "保存成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}

	/**
	 *   单个删除数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/del")
	@ResponseBody
	public PageData del() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		try {
			pageData.put("isDelete",1);
			pageData.put("updateTime", DateUtil.getTime());
			appliancesService.del(pageData);
			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "删除成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}


	/**
	 *   批量删除
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/dels")
	@ResponseBody
	public PageData dels() throws Exception{
		//返回结果
		PageData result = new PageData();
		//参数
		PageData pageData = getPageData();
		String[] ids = pageData.getString("ids").split(",");

		try {
			List<String> idList= new ArrayList<>(Arrays.asList(ids));

			pageData.put("updateTime", DateUtil.getTime());
			appliancesService.dels(idList);
			result.put("code", "00");
			result.put("data", "");
			result.put("msg", "删除成功");
		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}
		return result;

	}

	
	/**
	 * 分页功能
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getPageDemoList")
	@ResponseBody
	public String getPageDemoList()
			throws Exception {
		PageData pageData =getPageData();
		PageHelper.startPage(Integer.parseInt(pageData.getString("page"))  , Integer.parseInt(pageData.getString("limit")));//页面的位置和展示的数据条目数
		String  data="";
		try {

			List<PageData> all = appliancesService.findAll(pageData);
			PageInfo<PageData> pageInfo = new PageInfo<PageData>(all);
			data= DataUtil.getJsonData(pageInfo);

		} catch (Exception e) {
			logger.error(e.toString());
			throw new Exception(e.toString());
		}


		return data;
	}

	
}
